php implementation code to move the pointer to the initial location of the dataset [mysql_data_seek]

  • 2020-05-26 07:57:43
  • OfStack

 
<? 
// Start snipit 1 
$sql = "SELECT * from <table>"; 
$result = mysql_query($sql); 
while ($row = mysql_fetch_assoc($result)) { 
// do stuff with $row 
} 
mysql_data_seek($result, 0); // The point is here  
while ($row = mysql_fetch_assoc($result)) { 
// do other stuff with $row 
} 
?> 


Definition and usage

The mysql_data_seek() function moves the pointer to the internal result.

grammar

mysql_data_seek(data,row) parameter description
data required. Returns a result set of type resource. The result set is obtained from a call to mysql_query().
row required. The number of rows of the new result set pointer that you want to set. 0 indicates the first record.

instructions

mysql_data_seek() moves the row pointer inside the MySQL result specified by the data parameter to the specified line number.
Then a call to mysql_fetch_row() will return that 1 line.
row starts at 0. The values of row should range from 0 to mysql_num_rows-1.
But if the result set is empty (mysql_num_rows() == 0), moving the pointer to 0 will fail and an E_WARNING level error will be issued, mysql_data_seek() will return false.

The return value

Return true on success and false on failure.

Hints and comments

Note: mysql_data_seek() can only be used with mysql_query() 1, not mysql_unbuffered_query().

example
 
<?php 
$con = mysql_connect("localhost", "hello", "321"); 
if (!$con) 
{ 
die('Could not connect: ' . mysql_error()); 
} 
$db_selected = mysql_select_db("test_db",$con); 
$sql = "SELECT * from Person"; 
$result = mysql_query($sql,$con); 
print_r(mysql_fetch_row($result)); 
mysql_data_seek($result,3); 
print_r(mysql_fetch_row($result)); 
mysql_close($con); 
?> 

Output:
 
Array 
( 
[0] => Adams 
[1] => John 
[2] => London 
) 

Array 
( 
[0] => Carter 
[1] => Thomas 
[2] => Beijing 
) 


Related articles: